'use strict';

var controllers = angular.module('controllers', ['services']);

controllers.controller('QuestionsController', ['$scope', 'Question','Tag','Author','Answer', function($scope, Question, Tag, Author, Answer) {

	init();
	
	function init() {
	    $scope.currentQuestion = new Question();
	    $scope.questions = Question.query();
	    $scope.authors = Author.query();
	    $scope.tags = Tag.query();
	    $scope.firstAnswer = new Answer();
	    $scope.secondAnswer = new Answer();
	    $scope.thirdAnswer = new Answer();
		$scope.tagList = new Array();
	}
	
	function reset() {
		$scope.currentQuestion = new Question();
		$scope.firstAnswer = new Answer();
		$scope.secondAnswer = new Answer();
		$scope.thirdAnswer = new Answer();
		$scope.tagList = new Array();
	}
	
    $scope.cancel = function () {
        $scope.currentQuestion = new Question();
    };

    $scope.save = function (authorid) {
		console.log($scope.currentQuestion.id);
		if ($scope.currentQuestion.id == null) {
			$scope.saveAnswersOnQuestion();
			$scope.addAuthor(authorid);
			console.log($scope.tagList);
			$scope.currentQuestion.tags = new Array();
			console.log($scope.currentQuestion);
			$scope.currentQuestion.tags = $scope.tagList;
			Question.save($scope.currentQuestion);    		
			$scope.questions.push($scope.currentQuestion);
		}else {
			$scope.saveAnswersOnQuestion();
			$scope.addAuthor(authorid);
			$scope.currentQuestion.tags = $scope.tagList; 
			Question.update($scope.currentQuestion);
		}                             
        reset();
    };

    $scope.saveAnswersOnQuestion = function() {
		var answerList = new Array();
		$scope.firstAnswer.isTrue = true;
		answerList.push($scope.firstAnswer);
		answerList.push($scope.secondAnswer);
		answerList.push($scope.thirdAnswer);
		$scope.currentQuestion.answers = answerList;
    };
    
    $scope.addAuthor = function(id) {
    	var myAuthor = filterById($scope.authors,id);
    	$scope.currentQuestion.author = myAuthor;
    };
    
    $scope.addLabel = function(id) {
    	$scope.tagList.push(filterById($scope.tags,id));

    };
    
    $scope.removeLabel= function (tag) {
    	$scope.tagList.splice($scope.tagList.indexOf(tag), 1);
    };
    
    $scope.edit = function (question) {
    	$scope.currentQuestion = question;
    	$scope.firstAnswer = question.answers[0];
    	$scope.secondAnswer = question.answers[1];
    	$scope.thirdAnswer = question.answers[2];
    	$scope.tagList = question.tags;
    };

    $scope.remove = function (index, id) {
		Question.remove({questionId:id},
				function(successResult) {
					$scope.questions.splice(index, 1);
	    		}, function(errorResult) {
	    			window.alert("Diese Frage wird noch in einer QList verwendet");
	    });
    };
}]);


controllers.controller('AuthorController', ['$scope', 'Author', function($scope, Author) {

	init();
	
	function init() {
	    $scope.currentAuthor = new Author();
	    $scope.authors = Author.query();
	    $scope.showId = false;
	}

    $scope.cancel = function () {
        $scope.currentAuthor = new Author();
    };

    $scope.save = function () {
        var isNew = $scope.currentAuthor.id == null;
        if (isNew) {
            $scope.currentAuthor = Author.save($scope.currentAuthor);
            $scope.authors.push($scope.currentAuthor);
        } else {
            $scope.currentAuthor = Author.update($scope.currentAuthor);
        }
        $scope.cancel();
    };

    $scope.edit = function (author) {
    	$scope.currentAuthor = author;
    };

    $scope.remove = function (index, id) {
		Author.remove({authorId:id},
				   function(successResult) {
					$scope.authors.splice(index, 1);
			    }, function(errorResult) {
			    	window.alert("Failed: "+errorResult.status+"\n Dieser Author wird noch von einer Frage verwendet");
			    }  
			);
		
		
    };
}]);

controllers.controller('TagController', ['$scope', 'Tag', function($scope, Tag) {
    $scope.currentTag = new Tag();
    $scope.tags = Tag.query();

    $scope.save = function () {
    	console.log($scope.currentTag.id);
    	if ($scope.currentTag.id == null) {
    		$scope.currentTag = Tag.save($scope.currentTag);
    		$scope.tags.push($scope.currentTag);
		}else {
			$scope.currentTag = Tag.update($scope.currentTag);
		}
    	$scope.cancel();
    };

    
    $scope.edit = function (tag) {
    	$scope.currentTag = tag;
    };
    
    $scope.remove = function (index, id) {
			Tag.remove({tagId:id},
			   function(successResult) {
				$scope.tags.splice(index, 1);
		    }, function(errorResult) {
		    	window.alert("Failed: "+errorResult.status+"\n Dieser Tag wird noch von einer Frage verwendet");
		    }  
		);
    };
    
    $scope.cancel = function () {
        $scope.currentTag = new Tag();
    };

}]);

controllers.controller('QListsController', ['$scope', 'QList', 'Question', 'Sender', function($scope, QList, Question, Sender) {
    
	init();
	
	function init() {
		$scope.currentQList = new QList();
		$scope.questions = Question.query();
		$scope.qlists = QList.query();
		$scope.tempQuestions = new Array();
	}
	
    $scope.save = function () {
    	console.log($scope.currentQList.id);
    	if ($scope.currentQList.id == null) {
    		$scope.currentQList.questions = new Array();
    		$scope.currentQList.questions = $scope.tempQuestions;
    		QList.save($scope.currentQList);
    		$scope.qlists.push($scope.currentQList);
    	}else {
    		$scope.currentQList.questions = $scope.tempQuestions;
    		QList.update($scope.currentQList);
    		
		}
    	
    	$scope.currentQList = new QList();
    	$scope.tempQuestions = new Array();
    };

    $scope.remove = function (index, id) {
    	$scope.qlists.splice(index, 1);
    	QList.remove({qlistId:id});
    	
    };
    
    $scope.cancel = function () {
        init();
    };
    
    $scope.edit = function (qlist) {
    	$scope.currentQList = qlist;
    	$scope.tempQuestions = qlist.questions;
    };
    
    $scope.addQuestion = function (questionid) {
    	$scope.tempQuestions.push(filterById($scope.questions, questionid));
    };

    $scope.removeQuestion = function (qs){
    	$scope.tempQuestions.splice($scope.tempQuestions.indexOf(qs), 1);
    };
    
    $scope.send = function (qlist) {
    	Sender.update(qlist);
    };
}]);


function filterById(array, id) {
    return array.filter(function (object) {
        return object.id == id;
    })[0];
}
